// IM app 端的接口
import lodsah from 'lodash';

const ActionHelper = {
  // 客户端 action
  register: '注册',
  open_app: '打开app',
  active_time: '活跃时间',
};

const statisticsHelper = function(options) {
  const { request, endpoint, deviceInfo } = options;
  if (!(request && endpoint && deviceInfo)) {
    console.error('初始化数据统计，参数错误', { request, endpoint, deviceInfo });
  }

  const post = function(body) {
    console.log('发送统计数据', body);
    return request(endpoint, {
      method: 'post',
      body: JSON.stringify(body),
      headers: {
        'content-type': 'application/json',
      },
    })
      .then(response => response.json())
      .catch(console.warn);
  };

  return {
    register: ({ userId, phone, userCreatedAt }) => {
      const action = ActionHelper.register;
      const body = {
        action,
        userId,
        phone,
        userCreatedAt,
        ...deviceInfo,
      };
      return post(body);
    },

    open_app: ({ userId, phone, userCreatedAt }) => {
      const action = ActionHelper.open_app;
      const body = {
        action,
        userId,
        phone,
        userCreatedAt,
        ...deviceInfo,
      };
      return post(body);
    },

    active_time: ({ userId, phone, userCreatedAt, activeTime }) => {
      const action = ActionHelper.active_time;
      const body = {
        action,
        userId,
        phone,
        userCreatedAt,
        activeTime,
        ...deviceInfo,
      };
      return post(body);
    },
  };
};

export default statisticsHelper;
